﻿
using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.HalfHourCalSave.Common.Basic;
using DataWorks_Tools.MappModals.CalInputModals;
using DataWorks_Tools.MappModals.CalSaveModals;
using RainFlowandDamageTool.ComputingProcess;
using Yitter.IdGenerator;
using static DataWorks_Tools.BasicMethods.ReadfromCSV;

namespace DataWorks_Tools.HalfHourCalSave.Common.DataImport
{



    public static class Tools_StatisticImport
    {
        [Obsolete]
        public static async Task<List<calsave_statistic>> GetHalfHourStatisticAsync(CSVImport csvimport, HalfHourCalInputModel props, int binnumber)
        {
            List<calsave_statistic> statisticall = new List<calsave_statistic>();
            double nextmileage = 0;
            //每一列做一次统计
            if (props.importstatistic == 1)
            {
                for (int i = 1; i < csvimport.columncount; i++)
                {
                    calsave_statistic statisticentity = new calsave_statistic();
                    statisticentity.vehicle = props?.vehicle;
                    statisticentity.project = props?.project;
                    statisticentity.filename = csvimport.name;
                    statisticentity.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                    statisticentity.subdirectory = csvimport.subdirectory;
                    statisticentity.chantitle = csvimport.headers[i];
                    statisticentity.smax = csvimport.lists[i].Max();
                    statisticentity.smin = csvimport.lists[i].Min();
                    statisticentity.srange = Math.Abs(statisticentity.smax - statisticentity.smin);
                    double sumOfSquares = csvimport.lists[i].Sum(x => x * x);
                    double meanSquare = sumOfSquares / (csvimport.lists[i].Count - 1);
                    statisticentity.srms = Math.Sqrt(meanSquare);
                    double mean = csvimport.lists[i].Average();
                    statisticentity.smean = mean;
                    double variance = csvimport.lists[i].Sum(x => Math.Pow(x - mean, 2)) / (csvimport.lists[i].Count - 1);
                    statisticentity.svariance = variance;
                    statisticentity.std = Math.Sqrt(variance);
                    var linkedlst = csvimport.lists[i].GetLinkedRainflowfromList(binnumber);
                    statisticentity.damagek3 = linkedlst.GetAccumDamagefromLinkedList(3, 3);
                    statisticentity.damagek5 = linkedlst.GetAccumDamagefromLinkedList(5, 5);
                    //第一次算一下里程，后面就不用算了，每个通道都是同一个里程，所以加了一个判断条件为i==1
                    if (i == 1 || i == 2)
                    {
                        //把lat和lon的所有统计参数都改为各自的第一个数据，因为它们的统计值没有意义
                        statisticentity.smax = csvimport.lists[i][0];
                        statisticentity.smin = csvimport.lists[i][0];
                        statisticentity.smean = csvimport.lists[i][0];
                        statisticentity.std = csvimport.lists[i][0];
                        statisticentity.svariance = csvimport.lists[i][0];
                        statisticentity.srms = csvimport.lists[i][0];
                        statisticentity.srange = csvimport.lists[i][0];
                        //计算里程
                        if (i == 1)
                        {
                            var idx = Array.IndexOf(csvimport.headers, props?.chantitle_spd?.ReName());
                            if (idx != -1)
                            {
                                var singledistance = await Distance.ReturnSingleDistance(csvimport.lists[idx], csvimport.lists[0]);
                                nextmileage = singledistance.Sum();
                            }
                        }

                    }
                    statisticentity.mileage = nextmileage;
                    statisticall.Add(statisticentity);
                }

            }
            return statisticall;
        }

        /// <summary>
        /// 统计值及RP计数放在一起输出
        /// </summary>
        /// <param name="csvimport"></param>
        /// <param name="props"></param>
        /// <returns></returns>
        public static async Task<List<calsave_rplist>> GetHalfHourStatisticandRPAsync(CSVImport csvimport, HalfHourCalInputModel props,int binnumber)
        {
            //一个文件csvimport对应一个starp的数据集，数据集的数量就是通道的数量，也就是说一个通道就对应一个calsave_rplist
            List<calsave_rplist> starp = new List<calsave_rplist>();
            double nextmileage = 0;
            //每一列做一次统计
            if (props.importstatistic == 1)
            {
                for (int i = 1; i < csvimport.columncount; i++)
                {
                    calsave_rplist calsave_Rangepair = new calsave_rplist();
                    calsave_Rangepair.statistic.vehicle = props?.vehicle;
                    calsave_Rangepair.statistic.project = props?.project;
                    calsave_Rangepair.statistic.filename = csvimport.name;
                    calsave_Rangepair.statistic.datadate = Convert.ToDateTime(csvimport.datetimesecond);
                    calsave_Rangepair.statistic.subdirectory = csvimport.subdirectory;
                    calsave_Rangepair.statistic.chantitle = csvimport.headers[i];
                    calsave_Rangepair.statistic.smax = csvimport.lists[i].Max();
                    calsave_Rangepair.statistic.smin = csvimport.lists[i].Min();
                    calsave_Rangepair.statistic.srange = Math.Abs(calsave_Rangepair.statistic.smax - calsave_Rangepair.statistic.smin);
                    double sumOfSquares = csvimport.lists[i].Sum(x => x * x);
                    double meanSquare = sumOfSquares / (csvimport.lists[i].Count - 1);
                    calsave_Rangepair.statistic.srms = Math.Sqrt(meanSquare);
                    double mean = csvimport.lists[i].Average();
                    calsave_Rangepair.statistic.smean = mean;
                    double variance = csvimport.lists[i].Sum(x => Math.Pow(x - mean, 2)) / (csvimport.lists[i].Count - 1);
                    calsave_Rangepair.statistic.svariance = variance;
                    calsave_Rangepair.statistic.std = Math.Sqrt(variance);
                    var linkedlst = csvimport.lists[i].GetLinkedRainflowfromList(binnumber);
                    if(linkedlst?.Count>0)
                    {
                        calsave_Rangepair.rplst = linkedlst.ToList();
                        calsave_Rangepair.statistic.damagek3 = linkedlst.GetAccumDamagefromLinkedList(3, 3);
                        calsave_Rangepair.statistic.damagek5 = linkedlst.GetAccumDamagefromLinkedList(5, 5);

                        foreach (var item in calsave_Rangepair.rplst)
                        {
                            item.chantitle = csvimport.headers[i];  
                            item.vehicle = props.vehicle;  
                            item.project = props?.project;
                            item.filename= csvimport.name;
                            item.Damage = 0;
                            item.subdirectory = csvimport.subdirectory;
                            item.datadate= calsave_Rangepair.statistic.datadate;
                        }
                    }
                    //第一次算一下里程，后面就不用算了，每个通道都是同一个里程，所以加了一个判断条件为i==1
                    if (i == 1 || i == 2)
                    {
                        //把lat和lon的所有统计参数都改为各自的第一个数据，因为它们的统计值没有意义
                        calsave_Rangepair.statistic.smax = csvimport.lists[i][0];
                        calsave_Rangepair.statistic.smin = csvimport.lists[i][0];
                        calsave_Rangepair.statistic.smean = csvimport.lists[i][0];
                        calsave_Rangepair.statistic.std = csvimport.lists[i][0];
                        calsave_Rangepair.statistic.svariance = csvimport.lists[i][0];
                        calsave_Rangepair.statistic.srms = csvimport.lists[i][0];
                        calsave_Rangepair.statistic.srange = csvimport.lists[i][0];
                        //计算里程
                        if (i == 1)
                        {
                            var idx = Array.IndexOf(csvimport.headers, props?.chantitle_spd?.ReName());
                            if (idx != -1)
                            {
                                var singledistance = await Distance.ReturnSingleDistance(csvimport.lists[idx], csvimport.lists[0]);
                                nextmileage = singledistance.Sum();
                            }
                        }

                    }
                    calsave_Rangepair.statistic.mileage = nextmileage;
                    starp.Add(calsave_Rangepair);
                }

            }
            return starp;
        }


        public static async Task<List<calsave_permileage_split>> GetPerMileageStatisticAsync(CSVImport csvimport, BaseCalInputModel row, int binnumber)
        {
            List<calsave_permileage_split> statisticall = new List<calsave_permileage_split>();
            double nextmileage = 0;
            //string address = "error";
            //每一列做一次统计
            for (int i = 1; i < csvimport.columncount; i++)
            {
                calsave_permileage_split statisticentity = new calsave_permileage_split();
                statisticentity.key = YitIdHelper.NextId();
                statisticentity.vehicle = row?.vehicle;
                statisticentity.project = row?.project;
                statisticentity.filename = csvimport.name;
                statisticentity.datadate = Convert.ToDateTime(csvimport.datetime);
                statisticentity.createTime = statisticentity.datadate;
                statisticentity.subdirectory = csvimport.subdirectory;
                statisticentity.chantitle = csvimport.headers[i];
                statisticentity.smax = csvimport.lists[i].Max();
                statisticentity.smin = csvimport.lists[i].Min();
                statisticentity.srange = Math.Abs(statisticentity.smax - statisticentity.smin);
                double sumOfSquares = csvimport.lists[i].Sum(x => x * x);
                double meanSquare = sumOfSquares / (csvimport.lists[i].Count - 1);
                statisticentity.srms = Math.Sqrt(meanSquare);
                double mean = csvimport.lists[i].Average();
                statisticentity.smean = mean;
                double variance = csvimport.lists[i].Sum(x => Math.Pow(x - mean, 2)) / (csvimport.lists[i].Count - 1);
                statisticentity.svariance = variance;
                statisticentity.std = Math.Sqrt(variance);
                var linkedlst = csvimport.lists[i].GetLinkedRainflowfromList(binnumber);
                statisticentity.damagek3 = linkedlst.GetAccumDamagefromLinkedList(3, 3);
                statisticentity.damagek5 = linkedlst.GetAccumDamagefromLinkedList(5, 5);
                //第一次算一下里程，后面就不用算了，每个通道都是同一个里程，所以加了一个判断条件为i==1
                if (i == 1 || i == 2)
                {
                    //把lat和lon的所有统计参数都改为各自的第一个数据，因为它们的统计值没有意义
                    statisticentity.smax = csvimport.lists[i][0];
                    statisticentity.smin = csvimport.lists[i][0];
                    statisticentity.smean = csvimport.lists[i][0];
                    statisticentity.std = csvimport.lists[i][0];
                    statisticentity.svariance = csvimport.lists[i][0];
                    statisticentity.srms = csvimport.lists[i][0];
                    statisticentity.srange = csvimport.lists[i][0];
                    //计算里程
                    if (i == 1)
                    {
                        var idx = Array.IndexOf(csvimport.headers, row?.chantitle_spd?.ReName());
                        if (idx != -1)
                        {
                            var singledistance = await Distance.ReturnSingleDistance(csvimport.lists[idx], csvimport.lists[0]);
                            nextmileage = singledistance.Sum();
                        }
                        //先转一下百度地图坐标系再逆地理编码
                        //var newgps = GpsCoordinate.GpsToNewGps(csvimport.lists[1][0], csvimport.lists[2][0], 6);
                        //address = await GeoDecodingMethod.ReverseGeocoding(newgps._wgLat, newgps._wgLon);
                    }
                }
                statisticentity.mileage = nextmileage;
                //statisticentity.address = address;
                statisticall.Add(statisticentity);
            }
            return statisticall;
        }

       
    }
}
